package sk.stuba.fiit.arthur.web.mvc.controller;

/**
 * Document Controller.
 * 
 * @author Marek Hruska
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import sk.stuba.fiit.arthur.web.mvc.model.entity.DocumentEntity;
import sk.stuba.fiit.arthur.web.mvc.model.service.DocumentService;


/**
 * Document controller.
 * 
 * @author Marek Hruska
 */
@Controller
@SessionAttributes(value = {"documentEntity"})
public class DocumentController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String LIST_UNSIGNED_DOC_MODEL_KEY = "listUnsignedDocs";
	private static final String DOCUMENT_ENTITY = "documentEntity";
	
	protected static Logger logger = Logger.getLogger("DocumentController"); 
	
	@Autowired()
	private DocumentService documentService;
	
	/**
     * <p>Searches for all documents and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/document/list'.</p>
     */
    @RequestMapping(value="/document/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<DocumentEntity> search() {
    	logger.debug("Received request to show all documents"); 
    	
        return documentService.findDocuments();
    }
    
	/**
     * <p>Create new or update old document 
     * <code>DocumentEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/document/form'.</p>
     */
    @RequestMapping(value="/document/form", method=RequestMethod.GET)
    public @ModelAttribute(DOCUMENT_ENTITY) DocumentEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new DocumentEntity();
        return documentService.findDocumentById(id);
    }
    
	/**
     * <p>Save new or update old document 
     * <code>DocumentEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/document/saveForm'.</p>
     */
    @RequestMapping(value="/document/saveForm", method=RequestMethod.POST)
    public String saveDocumentForm(DocumentEntity documentEntity, Model model) {
    	if(documentEntity != null) 
    		documentService.save(documentEntity);
    	return "redirect:/document/list.html";
    }
    
    /**
     * <p>Deletes a document.</p>
     * 
     * <p>Expected HTTP POST and request '/document/delete'.</p>
     */
    @RequestMapping(value="/document/delete", method=RequestMethod.POST)
    public String delete(DocumentEntity documentEntity) {
    	logger.debug("Received request to delete document with id:"+ documentEntity.getId());
        documentService.delete(documentEntity);
        return "redirect:/document/list.html";
    }
    
    
    /**
     * <p>Searches for all unsigned documents and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/document/list'.</p>
     */
    @RequestMapping(value="/document/listUnsignedDocs", method=RequestMethod.GET)
    public @ModelAttribute(LIST_UNSIGNED_DOC_MODEL_KEY) Collection<DocumentEntity> getUns() {
    	logger.debug("Received request to show all unsigned documents"); 
    	return documentService.getUnsignedDocuments("TU POJDE STRING");
    }
}
